package com.lzp.systemopt.camera.activity

import android.Manifest
import android.os.Bundle
import android.util.Log
import com.base.BaseActivity
import com.imageloader.ImageLoader
import com.lzp.systemopt.camera.CameraManager
import com.lzp.systemopt.camera.CaptureListener
import com.lzp.systemopt.databinding.CameraActivityBinding
import com.utils.LogUtils
import com.utils.permission.PermissionUtils
import java.io.File

/**
 * @description:
 * @author: lxp
 * @date: 2022/1/22 下午4:39
 */
class CameraActivity : BaseActivity() {

    private val binding by lazy { CameraActivityBinding.inflate(layoutInflater) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
    }

    override fun initView() {
        super.initView()
        PermissionUtils.getPermission(this, arrayOf(Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO))
        binding.cameraView.initCamera(this)
        binding.cameraView.startCapturePicPreView()
       // binding.cameraView.startCaptureVideoPreview()
    }

    override fun setListener() {
        super.setListener()
        binding.cameraView.setCaptureListener(mCaptureListener)
        binding.capturePic.setOnClickListener {
            binding.cameraView.capturePic()
        }

        binding.captureVideo.setOnClickListener {
            binding.cameraView.captureVideo()
        }
        binding.changeCamera.setOnClickListener {
            binding.cameraView.requestSwitchCamera()
        }
        binding.stopCaptureVideo.setOnClickListener {
            binding.cameraView.stopCaptureVideo()
        }
    }

    private val mCaptureListener = object :CaptureListener{
        override fun onStartCapture() {

        }

        override fun onCaptureSuccess(file: File) {
            LogUtils.d("onCaptureSuccess: ${file.absolutePath}")
            runOnUiThread {
                ImageLoader.get(baseContext).display(file.absolutePath).into(binding.resultIv)
            }
        }
    }
    override fun onDestroy() {
        CameraManager.getInstance().releaseCamera("activity finish")
        super.onDestroy()
    }
}